<?php
namespace App\Transformers;

use League\Fractal\TransformerAbstract;
use App\Model\ProductCategory;
use App\Model\ProductSaleDay;
use App\Model\ProductSaleMonth;
//use Illuminate\Support\Facades\DB;

class ProductCategoryTransformer extends TransformerAbstract{

    public function transform(ProductCategory $item){
        return [
            'id' => $item->id,
            'bistro_id' => $item->bistro_id,
            'name' => $item->name
        ];
    }

    public static function forBackendStatis() {
        return function (ProductCategory $item){
            return [
                'id' => $item->id,
                'bistro_id' => $item->bistro_id,
                'name' => $item->name,
                'day' => ProductCategoryTransformer::forStaticToday($item),
                'month' => ProductCategoryTransformer::forStaticThisMonth($item)
            ];
        };
    }

    public static function forBackendDetail() {
        return function (ProductCategory $item){
            return [
                'id' => $item->id,
                'bistro_id' => $item->bistro_id,
                'name' => $item->name,
                'day' => ProductCategoryTransformer::forStaticDay($item)
//                'month' => ProductCategoryTransformer::forStaticThisMonth($item)
            ];
        };
    }
//
//    private static function forStaticDay($item) {//按天统计结果
//        $products = $item->product()->get()->toArray();
//        $product_ids = '';
//        foreach ($products as $p) {
//           $product_ids .= $p['id'].',';
//        }
//        $product_ids = substr($product_ids,0, -1);
//        $sql = "select sum(sale_count) as sum_sale_count, sum(total_amount) as sum_total_amount,date from product_sale_days where product_id in ({$product_ids}) group by date ";
//        $result = DB::select($sql);
//        return $result;
//
////        $day_count = ProductSaleDay::whereIn('product_id', $product_ids)->groupby('date')->sum('sale_count');
////        $day_total_amount = ProductSaleDay::whereIn('product_id', $product_ids)->groupby('date')->sum('total_amount');
////        return [
////            'day_sale_count' => $day_count,
////            'day_total_amount' => $day_total_amount
////        ];
//    }





    private static function forStaticToday($item) {//今天
        $date = date('Y-m-d');
        $products = $item->product()->get()->toArray();
        $product_ids = [];
        foreach ($products as $p) {
            array_push($product_ids, $p['id']);
        }

        $day_count = ProductSaleDay::whereIn('product_id', $product_ids)->where('date', $date)->sum('sale_count');
        $day_total_amount = ProductSaleDay::whereIn('product_id', $product_ids)->where('date', $date)->sum('total_amount');
        return [
            'day_sale_count' => $day_count,
            'day_total_amount' => $day_total_amount
        ];
    }

    private static function forStaticThisMonth($item) {//当月
        $month = date('Y-m');
        $products = $item->product()->get()->toArray();
        $product_ids = [];
        foreach ($products as $p) {
            array_push($product_ids, $p['id']);
        }
        $month_count = ProductSaleMonth::whereIn('product_id', $product_ids)->where('month', $month)->sum('sale_count');
        $month_total_amount = ProductSaleMonth::whereIn('product_id', $product_ids)->where('month', $month)->sum('total_amount');
        return [
            'month_sale_count' => $month_count,
            'month_total_amount' => $month_total_amount
        ];

    }

}